package com.aiti.lulian.controller.newCard;

import cn.hutool.core.exceptions.ExceptionUtil;
import com.aiti.base.core.exception.BusinessException;
import com.aiti.base.core.utils.message.Message;
import com.aiti.lulian.service.newCard.CardVersionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * description:
 * auth fcb
 * date 2025-08-15 14:34
 **/
@RestController
@RequestMapping("/new-card/user-version")
@Api(tags = "用户名片版本接口")
@Slf4j
public class UserVersionController {

    @Autowired
    private CardVersionService cardVersionService;

    @PutMapping("/insert-try-user")
    @ApiOperation("插入用户试用版本")
    public Message insertTryUser(@RequestParam("userId") String userId) {
        try {
            cardVersionService.insertTryUser(userId);
            return Message.success();
        }catch (Exception e) {
            if(e instanceof BusinessException)  {
                return Message.fail(e.getMessage());
            }
            log.error("插入用户试用版本异常：{}", ExceptionUtil.stacktraceToString(e));
            return Message.fail("操作失败");
        }
    }

    @PutMapping("/insert-enterprise-try-user")
    @ApiOperation("插入企业用户试用版本")
    @Deprecated
    public Message insertEnterpriseTryUser(@RequestParam("userId") String userId) {
        try {
            cardVersionService.insertEnterpriseTryUser(userId);
            return Message.success();
        }catch (Exception e) {
            if(e instanceof BusinessException)  {
                return Message.fail(e.getMessage());
            }
            log.error("插入用户试用版本异常：{}", ExceptionUtil.stacktraceToString(e));
            return Message.fail("操作失败");
        }
    }

    @GetMapping("/query-try-user")
    @ApiOperation("查询用户是否已试用")
    public Message queryTryUser(@RequestParam("userId") String userId) {
        return Message.success(cardVersionService.queryTryUserCount(userId));
    }


    @PutMapping("/update-user-version")
    @ApiOperation("更新用户名片版本")
    public Message updateUserVersion(@RequestParam("userId") String userId,
                                     @RequestParam("cardVersion") String cardVersion,
                                     @RequestParam("offset") Integer offset,
                                     @RequestParam("unit") String unit) {
        try {
            cardVersionService.setUserVersion(userId, cardVersion, offset, unit);
            return Message.success();
        }catch (Exception e) {
            if(e instanceof BusinessException)  {
                return Message.fail(e.getMessage());
            }
            log.error("更新用户名片版本异常：{}", ExceptionUtil.stacktraceToString(e));
            return Message.fail("操作失败");
        }
    }

    @GetMapping("/get-user-version-info")
    @ApiOperation("获取用户名片版本信息")
    public Message getUserVersionInfo(@RequestParam("userId") String userId) {
        return Message.success(cardVersionService.getUserVersionInfo(userId));
    }
}
